#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
ll n;
ll num[100010];
ll c[20];
ll f(ll x){
    ll cnt=0;
    for(ll i=0;i<4;i++){
        x/=10;
    }
    for(ll i=0;i<8;i++){
        c[i]=x%10;
        x/=10;
    }
    ll sum=0;
    for(ll i=7;i>=0;i--){
        sum=sum*10+c[i];
    }
    return sum;
}

bool judge(ll a,ll b){
    ll x1=f(a);
    ll x2=f(b);
    if(x1==x2){
        return a>b;
    }
    return x1>x2;
}

int main()
{
    IOS;
    cin>>n;
    for(ll i=0;i<n;i++){
        cin>>num[i];
    }
    sort(num,num+n,judge);
    for(ll i=0;i<n;i++){
        printf("%018lld\n",num[i]);
    }
    getchar();
    getchar();
    return 0;
}
